//
//  YHYLocalViewController.m
//  TTMusic
//
//  Created by yanghuiyan on 16/4/1.
//  Copyright © 2016年 cpy. All rights reserved.
//

#import "YHYLocalViewController.h"
#import "YHYLoadTwoTableViewCell.h"
#import "CPYSONGSModel.h"
#import "CPYDiscoveryPageSongListModel.h"
#import "CPYDiscoveryPageAuditionListModel.h"

@interface YHYLocalViewController ()<UITableViewDelegate, UITableViewDataSource, TTMusicSinglePlayerViewControllerDelegate>

@property (nonatomic, strong) TTMusicBaseTableView *mvTableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) CPYDataBaseTool *dataBase;
@property (nonatomic, strong) MBProgressHUD *HUD;
@property (nonatomic, strong) UIImageView *emptyImageview;

@end

@implementation YHYLocalViewController

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated {
    if (self.dataSource.count == 0) {
        
        self.mvTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        self.emptyImageview =[[UIImageView alloc] initWithFrame:CGRectMake((SysWidth *20)/375, (SysHeight *200)/667, (SysWidth *335)/375, (SysHeight *335)/667)];
        
        self.emptyImageview.image = [UIImage imageNamed:@"10"];
        
        [self.view addSubview:self.emptyImageview];
        
        // 否则隐藏
    }else{
        
        self.emptyImageview.hidden = YES;
        
    }
}
- (void)reloadata {
    CPYDataBaseTool *dataBase = [CPYDataBaseTool dataBaseManager];
    [dataBase openDataBase];
    [dataBase createTable];
    _dataSource = [_dataBase selectDataBase].songs;
    [_mvTableView reloadData];
    [dataBase closeDataBase];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ] style:UIBarButtonItemStylePlain target:self action:@selector(leftItemAction:)];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.view.backgroundColor = [UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    self.dataSource = [NSMutableArray array];
    self.dataBase = [CPYDataBaseTool dataBaseManager];
    [_dataBase openDataBase];
    [_dataBase createTable];
    _dataSource = [[CPYDataBaseTool dataBaseManager] selectDataBase].songs;
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [TTMusicSinglePlayerViewController sharePlayViewController].delegate = self;

    self.mvTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mvTableView = [[TTMusicBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SysWidth, SHORTHEIGHT) style:UITableViewStylePlain];
    [self.mvTableView registerClass:[YHYLoadTwoTableViewCell class] forCellReuseIdentifier:@"YHYLoadTwoTableViewCellIdentifier"];
    
    self.mvTableView.delegate = self;
    self.mvTableView.dataSource = self;
    [self.view addSubview:_mvTableView];
}
- (void) leftItemAction:(UIBarButtonItem *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    NSLog(@"%ld", [self.dataBase selectDataBase].songs.count);
//    return [self.dataBase selectDataBase].songs.count;
    return self.dataSource.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YHYLoadTwoTableViewCell *cell = [self.mvTableView dequeueReusableCellWithIdentifier:@"YHYLoadTwoTableViewCellIdentifier"];
    CPYDiscoveryPageSongListModel *songDetailModel = _dataSource[indexPath.row];
    cell.bigLabel.text =  songDetailModel.name;
    cell.smallLabel.text = songDetailModel.singerName;
    [cell.songImageView sd_setImageWithURL:[NSURL URLWithString:songDetailModel.picUrl]];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 55;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CPYDataBaseTool *dataBase = [CPYDataBaseTool dataBaseManager];
    [dataBase openDataBase];
    [dataBase createTable];
    
    [TTMusicAVPlayer shareTTMusicAVPlayer].local = indexPath.item;
    [[TTMusicAVPlayer shareTTMusicAVPlayer] playWithURL:dataBase.selectDataBase];
    [dataBase closeDataBase];
}


#pragma mark - 删除
//重写系统提供按钮的方法
- (void)setEditing:(BOOL)editing animated:(BOOL)animated {
    //首先让父类调用
    [super setEditing:editing animated:animated];
    //让tableView处于编辑状态下
    [self.mvTableView setEditing:YES animated:YES];
}

#pragma mark - 编辑tableView
//设置删除样式
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewCellEditingStyleDelete;
}

//是否允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

//完成编辑
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"------------------------%ld--------------------", indexPath.row);
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        // 先移除数据 再删除行
        CPYDiscoveryPageSongListModel *songDetailModel = _dataSource[indexPath.row];
        [self.dataSource removeObjectAtIndex:indexPath.row];
    
        [self.mvTableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
        
        [self.dataBase deleteDataBase:[NSString stringWithFormat:@"%@", songDetailModel.songId]];
        
        self.dataSource = [_dataBase selectDataBase].songs;
        
        if (self.dataSource.count == 0) {
            
            self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            
            //设置类型
            self.HUD.mode = MBProgressHUDModeText;
            
            //设置文字
            self.HUD.labelText = @"暂时没有收藏了┗|｀O′|┛ 嗷~~";
            
        }
        
        
        
        [self.mvTableView reloadData];
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
